﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Leikjavefurinn.Repo
{
    public class UserExtensionRepo
    {
        private GameDBDataContext db = new GameDBDataContext();

        public IQueryable<UserExtention> FindAllUserExtensions()
        {
            return db.UserExtentions;
        }

        public UserExtention GetUserExtension(System.Guid id)
        {
            return db.UserExtentions.SingleOrDefault(d => d.User == id);
        }

        //Næ í User í UserExtention með því að vita UserName í Users 
        public UserExtention GetUserExtension(string userName)
        {
            var result = (from ux in db.UserExtentions
                          join u in db.Users on
                          ux.User equals u.UserId
                          where u.UserName == userName
                          select ux).SingleOrDefault();

            return result;
        }

        //Bý til nýtt UserExtention með því að vita UserName í Users
        public void CreateUserExtension(string userName)
        {
            var user = db.Users.SingleOrDefault(n => n.UserName == userName);

            db.UserExtentions.InsertOnSubmit(new UserExtention { User = user.UserId });
            db.SubmitChanges();

        }


        public void Add(UserExtention user)
        {
            db.UserExtentions.InsertOnSubmit(user);
        }

        public void Update(UserExtention user)
        {

            db.GetChangeSet().Updates.Add(user);
        }

        public void Delete(UserExtention user)
        {
            db.UserExtentions.DeleteOnSubmit(user);
        }

        public void Save()
        {
            db.SubmitChanges();
        }

    }
}
